<template>
    <!-- 新增弹出框 -->
    <el-dialog v-bind:title="titleName" :visible="Visible" :modal="true" width="30%" :show-close="true"
               :close-on-click-modal="false" :destroy-on-close="true"  :before-close="handleDialogClose">
        <el-form ref="rulesForm" :model="data"  :rules="rules" label-width="80px">
            <el-form-item label="代码值" prop="dict_code" style="margin-bottom:15px;">
                <el-input v-model="data.dict_code"></el-input>
            </el-form-item>
            <el-form-item label="代码名称" prop="dict_name" style="margin-bottom:10px;">
                <el-input v-model="data.dict_name"></el-input>
            </el-form-item>
            <el-form-item label="代码类型" prop="dict_type" style="margin-bottom:15px;">
                <el-input v-model="data.dict_type"></el-input>
            </el-form-item>
            <el-form-item label="类别名称" prop="type_name" style="margin-bottom:15px;">
                <el-input v-model="data.type_name"></el-input>
            </el-form-item>
            <el-form-item label="排序" style="margin-bottom:15px;">
                <el-input v-model="data.dict_order"></el-input>
            </el-form-item>
            <el-form-item label="状态" style="margin-bottom:15px;">
                <input type="radio" id="qiy" value="1" style="margin-left:0px" v-model="data.dict_status">
                <label for="qiy">启用</label>
                <input type="radio" id="jiny" value="0" style="margin-left:50px" v-model="data.dict_status">
                <label for="jiny">禁用</label>
            </el-form-item>
            <el-form-item label="上级编号" style="margin-bottom:15px;">
                <el-input v-model="data.dict_parent"></el-input>
            </el-form-item>
            <el-form-item label="备注" style="margin-bottom:15px;">
                <el-input v-model="data.dict_remark"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="click_Close">关 闭</el-button>
                <el-button type="primary" @click="clicl_Save('rulesForm')">保 存</el-button>
            </span>
    </el-dialog>
</template>

<script>
    export default {
        name: "edit",
        inject: ['refresh', 'search', 'reLoadDict'],
        props: {
            titleName: {
                type: String,
                default: '新增'
            },
            Visible: {
                type:Boolean
            },
            data: {
                dict_pk: '',
                dict_code: '',
                dict_name: '',
                dict_type: '',
                type_name: '',
                dict_order: '',
                dict_status: '0',
                dict_time: '',
                dict_remark: '',
                dict_parent: '',
                dict_number:''
            }
        },
        data(){
            return {
                // Visible: true,
                url_save: '/auth/dict/dictSave',
                url_edit: '/auth/dict/dictUpdate',
                //    在return 后面；
                rules: {
                    // 要以数组形式展示
                    dict_code: [
                        { required: true, message: "请输入代码值", trigger: "change" }
                    ],
                    dict_name: [
                        { required: true, message: "请输入代码名称", trigger: "change" },
                        { min: 1, max: 100, message: "长度在 1 到 100 个字符", trigger: "blur" }
                    ],
                    dict_type: [
                        { required: true, message: "请输入代码类型", trigger: "change" },
                        { min: 1, max: 30, message: "长度在 1 到 30 个字符", trigger: "blur" }
                    ],
                    type_name: [
                        { required: true, message: "请输入类别名称", trigger: "change" },
                        { min: 1, max: 30, message: "长度在 1 到 30 个字符", trigger: "blur" }
                    ]
                }
            }
        },
        methods:{
            //关闭窗口方法
            click_Close(){
                this.Visible=false;
                this.search();
            },
            //保存数据方法
            clicl_Save(formName){
                if (this.data.dict_pk===''|| this.data.dict_pk === undefined) {
                    this.url = this.url_save
                }else {
                    this.url = this.url_edit
                }
                this.$formValidate(this.$refs[formName], valid => {
                    if (valid) {
                        console.log("success submit!!");
                        this.$post(this.url, this.data).then(res => {
                            this.$message({type : res.code, message: res.msg,});
                            console.log(res.msg);
                            if ("操作成功" == res.msg){
                                //成功以后清空表单数据
                                this.data.dict_code='';
                                this.data.dict_name='';
                                this.data.dict_type='';
                                this.data.type_name='';
                                this.data.dict_number =  this.data.dict_order;
                                this.data.dict_number = parseInt(this.data.dict_number)+1;
                                this.data.dict_order=this.data.dict_number;
                                this.reLoadDict();
                            }

                           // this.Visible=false;

                            //刷新列表页的数据
                            //this.search();
                        })
                    }else{
                        console.log("error submit!!");
                        return false;
                        /*   this.$message({
                               type :res.code,
                               message: res.msg,
                           });*/
                    }
                })
            },
            //右上角关闭事件
            handleDialogClose(){
                this.click_Close();
            }
        }
    }
</script>

<style scoped>

</style>